package com.dyh.quickdemozwei.forCollector;

import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class ForCollecotr {


//    public

    public static class SetCollector<T,E> extends RoundCollector<Set<E>,E>{
        Set<E> set;
        Function<T,E> function;



    }

    public static class RoundCollector<T,E>{
        private  Supplier<T> iniSupplier;
        private  BiConsumer<T,E> consumer;

        public  static <T,E> RoundCollector<T,E> from(Supplier<T> iniSupplier, BiConsumer<T, E> consumer){
            RoundCollector collector = new RoundCollector<>();
            collector.iniSupplier = iniSupplier;
            collector.consumer = consumer;
            return collector;
        }



//        private RoundCollector(Supplier<T> iniSupplier, BiConsumer<T, E> consumer) {
//            this.iniSupplier = iniSupplier;
//            this.consumer = consumer;
//
//           result = iniSupplier.get();
//        }

        private T result;

        public T getResult() {
            return result;
        }

        public void   collector(E e){
            consumer.accept(result,e);
        };

//        public BiConsumer<T, E> getConsumer() {
//            return consumer;
//        }
    }

    public static <E> void pass(List<E> list,RoundCollector<?,E>... collectors){
        for (E e : list) {
            for (RoundCollector<?, E> collector : collectors) {
                collector.collector(e);
            }
        }
    }

    @Test
    public void test(){
        List<Integer> list = Lists.newArrayList(1,2,3);
        RoundCollector collector1 = RoundCollector.from(ArrayList::new,(BiConsumer<ArrayList, Integer>) (c, o) -> c.add(o*2));
        RoundCollector collector2 = RoundCollector.from(HashMap::new,(BiConsumer<HashMap, Integer>) (c, o) -> c.put(""+o,o*2));

        pass(list,collector1,collector2);

        System.out.println(collector1.getResult());
    }
}
